<template>
  <div>
    <div v-if="mode === 'DESIGN'">
      <el-input disabled :placeholder="$t(placeholder)" :maxlength="maxlength"/>
    </div>
    <div v-else-if="mode === 'PC' && !readonly" >
      <el-input clearable v-model="_value" :placeholder="$t(placeholder)" :maxlength="maxlength">
        <template #append v-if="enableScan">
          <el-button @click="scanCode" icon="fullScreen"></el-button>
        </template>
      </el-input>
      <scan-code pcMode v-model="visible" @ok="scanOk"></scan-code>
    </div>
    <div v-else-if="mode === 'MOBILE' && !readonly">
      <field v-model="_value" clearable :placeholder="$t(placeholder)" :maxlength="maxlength">
        <template #right-icon v-if="enableScan">
          <el-icon @click="scanCode"><FullScreen /></el-icon>
        </template>
      </field>
      <scan-code v-model="visible" @ok="scanOk"></scan-code>
    </div>
    <div v-else>
      <!-- {{ _value }} -->
      <el-input clearable v-model="_value" :disabled="readonly" :maxlength="maxlength">
        <template #append v-if="enableScan">
          <el-button @click="scanCode" icon="fullScreen"></el-button>
        </template>
      </el-input>
    </div>
  </div>
</template>

<script>
import componentMinxins from '../ComponentMinxins'
import { Field } from 'vant'
import ScanCode from '@/components/common/ScanCode.vue'
export default {
  mixins: [componentMinxins],
  name: 'TextInput',
  components: { Field, ScanCode },
  props: {
    modelValue: {
      type: String,
      default: null,
    },
    placeholder: {
      type: String,
      default: 'webAuto.enterContent',
    },
    enableScan: {
      type: Boolean,
      default: false,
    },
    defaultValue: {
      type: String,
      default: '',
    },
    maxlength: {
      type: Number,
      default: 255,
    },
  },
  data() {
    return {
      visible: false,
    }
  },
  // 给页面添加默认值
  mounted() {
    if (this.defaultValue && !this.modelValue) {
      this._value = this.defaultValue;
      this.$emit('update:modelValue', this.defaultValue);
    }
  },
  methods: {
    scanCode() {
      this.visible = true
    },
    scanOk(code) {
      this._value = code
      showSuccessToast(this.$t('webAuto.ScanCodeSuccessfully'))
    },
  },
  emits: ['update:modelValue'],
}
</script>

<style lang="less" scoped></style>
